#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
复制对话相关的前后端文件内容到一个文本文件中
每个文件内容前加上文件路径
"""

import os

def copy_chat_files_to_text(output_file: str) -> None:
    """
    将对话相关的前后端文件内容复制到一个文本文件中
    
    Args:
        output_file: 输出文本文件路径
    
    Returns:
        None
    """
    # 定义要复制的对话相关文件列表
    chat_files = [
        # 前端文件
        "web/src/features/chat/pages/ChatPage.vue",
        "web/src/features/welcome/pages/Welcome.vue",

        # 后端文件
        "app/api/v1/conversations/routes.py",
        "app/api/v1/conversations/services.py",
        "app/api/v1/conversations/schemas.py",
        "app/services/history_service.py",
        "app/services/mcp_server/mcp_client.py",
        "app/services/mcp_server/mcp_manager.py",
        "app/services/mcp_server/mcp_service.py",
        "app/services/agent_service.py",
    ]
    
    # 获取项目根目录
    project_root = os.path.dirname(os.path.abspath(__file__))
    
    with open(output_file, 'w', encoding='utf-8') as out_f:
        for file_path in chat_files:
            # 构建完整文件路径
            full_path = os.path.join(project_root, file_path)
            
            # 检查文件是否存在
            if not os.path.exists(full_path):
                print(f"文件不存在: {full_path}")
                continue
            
            try:
                # 读取文件内容
                with open(full_path, 'r', encoding='utf-8') as in_f:
                    content = in_f.read()
                
                # 写入文件路径和内容
                out_f.write(f"{'='*50}\n")
                out_f.write(f"文件路径: {file_path}\n")
                out_f.write(f"{'='*50}\n")
                out_f.write(content)
                out_f.write(f"\n{'='*50}\n\n")
                
                print(f"已处理: {file_path}")
            except Exception as e:
                print(f"处理文件时出错 {file_path}: {str(e)}")
    
    print(f"\n所有文件已复制到: {output_file}")
    # 打印文件字符串长度
    try:
        with open(output_file, 'r', encoding='utf-8') as f:
            content = f.read()
        print(f"文件字符串长度: {len(content)} 字符")
    except Exception as e:
        print(f"读取文件长度时出错: {str(e)}")

if __name__ == "__main__":
    # 调用函数，输出到chat_files.txt
    copy_chat_files_to_text("chat_files.txt")
